<?php
require_once('Kbs/Controller/Action.php');

class Admin_PageController extends Kbs_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
        $this->_forward('list');
    }

    public function listAction()
    {
        $businessObj = new Kbs_Model_Business_Page();
        $this->view->pages = $businessObj->fetchAll();
    }

    // to edit the post
    public function editAction()
    {
        $localeObj = new Kbs_Model_Business_Locale();
        $this->view->locales = $localeObj->fetchAll();

        if ($this->getRequest()->isGet()) {
            if ($this->_hasParam('id')) {
                $id = $this->_getParam('id');

                $businessObj = new Kbs_Model_Business_Page();
                $page = $businessObj->find($id);

                $this->view->page = $page;
                $this->view->pageId           = $page->getPageId();
                $this->view->pageLinkUrl      = $page->getPageLinkUrl();
                $this->view->pageCreatedAt    = $page->getPageCreatedAt();
                $this->view->pageUpdatedAt    = $page->getPageUpdatedAt();
                $this->view->pagePublished    = $page->getPagePublished();

                $details = $page->fetchDetails();

                if (!empty($details)) {
                    foreach ($details as $detail) {
                        $this->view->{'detailId_' . $detail->detailLocaleId}        = $detail->detailId;
                        $this->view->{'detailLinkLabel_' . $detail->detailLocaleId} = $detail->detailLinkLabel;
                        $this->view->{'detailContent_' . $detail->detailLocaleId}   = $detail->detailContent;
                    }
                }
            }
        }
    }

    // to save the post
    public function saveAction()
    {
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();

            $postObj = new Kbs_Model_Business_Page();
            $psotId = $postObj->savePageAndDetails($data);

            $this->_redirect('/admin/page');

        } else {
            $this->_redirect('/admin');
        }
    }

    // to delete the post
    public function deleteAction()
    {
        if ($this->getRequest()->isGet()) {
            $id = $this->_getParam('id');

            $postObj = new Kbs_Model_Business_Page();
            $postObj->deletePageAndDetails($id);

            $this->_redirect('/admin/page');
        } else {
            $this->_redirect('/admin');
        }
    }

}